package com.hxh.generater.springBoot.simple.gen;

import com.hxh.config.Constants;
import com.hxh.entity.Project;
import com.hxh.entity.po.ClassInfo;
import com.hxh.generater.springBoot.GenerationTableInfo;
import com.hxh.generater.springBoot.genBase.GenLoginAndPermission;
import com.hxh.generater.springBoot.util.ProcessWriteLine;
import com.hxh.util.GenUtil;
import org.apache.commons.lang3.StringUtils;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

/**
 * @ProjectName: AICode
 * @Package: com.hxh.generater.springBoot.simple.gen
 * @ClassName: GenSimpleLoginAndPermission
 * @Author: HXH
 * @Description:
 * @Date: 2024/5/21 15:04
 */
public class GenSimpleLoginAndPermission implements GenLoginAndPermission {
    @Override
    public void generationKaptchaConfiguration(Project project) {
        try {
            File classFile = GenUtil.newFile(project, "config", "KaptchaConfiguration");
            FileWriter fileWriter = new FileWriter(classFile);
            BufferedWriter writer = new BufferedWriter(fileWriter);

            File template = new File(Constants.templatePathPrefix + "simple\\template\\config\\KaptchaConfiguration");
            BufferedReader br = new BufferedReader(new FileReader(template));
            String line;
            Map<String, String> replaceWord = new HashMap<>(1);
            replaceWord.put("GroupId", project.getBackendProject().getProjectGroup());
            ProcessWriteLine processWriteLine = new ProcessWriteLine(replaceWord);
            while ((line = br.readLine()) != null) {
                processWriteLine.writeLine(line, writer);
            }
            br.close();

            writer.close();
            fileWriter.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * @param project           :
     * @param loginTable        : 登录表名
     * @param usernameFieldName : 账号的字段名
     * @param passwordFieldName : 密码的字段名
     * @return void
     * @author HXH
     * @description: 登录相关代码
     * @date 2024/5/21
     **/
    @Override
    public void generationLoginAbout(Project project, String loginTable, String usernameFieldName, String passwordFieldName) {
        generationLoginController(project,loginTable);
        generationLoginInterceptorClass(project,loginTable);
        generationSessionConfigurationClass(project);
    }

    /**
     * @author HXH
     * @description:
     * @date  2024/5/21
     * @param project:
     * @param loginTable: 登录用户表 校验字段是username 和 password
     * @return void
     **/
    private static void generationLoginInterceptorClass(Project project, String loginTable) {
        try {
            ClassInfo loginUserClassInfo = GenerationTableInfo.getTableInfo(project, null, loginTable);
            File classFile = GenUtil.newFile(project, "config", "LoginInterceptor");

            String className = loginUserClassInfo.getClassName();
            String userEntityName = StringUtils.uncapitalize(className);

            FileWriter fileWriter = new FileWriter(classFile);
            BufferedWriter writer = new BufferedWriter(fileWriter);

            File template = new File(Constants.templatePathPrefix + "simple\\template\\config\\LoginInterceptor");
            BufferedReader br = new BufferedReader(new FileReader(template));
            String line;

            Map<String, String> replaceWord = new HashMap<>(3);
            replaceWord.put("GroupId", project.getBackendProject().getProjectGroup());
            replaceWord.put("userClassName",className);
            replaceWord.put("userEntityName",userEntityName);
            ProcessWriteLine processWriteLine = new ProcessWriteLine(replaceWord);
            while ((line = br.readLine()) != null) {
                processWriteLine.writeLine(line,writer);
            }
            br.close();

            writer.close();
            fileWriter.close();

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * @author HXH
     * @description:   权限拦截
     * @date  2024/5/21
     * @param project:
     * @return void
     **/
    private static void generationSessionConfigurationClass(Project project){
        try {
            File classFile = GenUtil.newFile(project, "config", "SessionConfiguration");
            FileWriter fileWriter = new FileWriter(classFile);
            BufferedWriter writer = new BufferedWriter(fileWriter);

            File template = new File(Constants.templatePathPrefix + "simple\\template\\config\\SessionConfiguration");
            BufferedReader br = new BufferedReader(new FileReader(template));
            String line;
            Map<String, String> replaceWord = new HashMap<>(1);
            replaceWord.put("GroupId", project.getBackendProject().getProjectGroup());
            ProcessWriteLine processWriteLine = new ProcessWriteLine(replaceWord);
            while ((line = br.readLine()) != null) {
                processWriteLine.writeLine(line, writer);
            }
            br.close();
            writer.close();
            fileWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * @author HXH
     * @description:
     * @date  2024/5/21
     * @param project:
     * @param loginTable: 登录用户表 校验字段是username 和 password
     * @return void
     **/
    private static void generationLoginController(Project project, String loginTable){
        try {
            ClassInfo loginClassInfo = GenerationTableInfo.getTableInfo(project, null, loginTable);
            String className = loginClassInfo.getClassName();

            String serviceName = className + "Service";
            String serviceNameEntity = StringUtils.uncapitalize(serviceName);
            String classNameEntity = StringUtils.uncapitalize(className);

            File classFile = GenUtil.newFile(project, "controller", "LoginController");

            FileWriter fileWriter = new FileWriter(classFile);
            BufferedWriter writer = new BufferedWriter(fileWriter);

            File template = new File(Constants.templatePathPrefix + "simple\\template\\LoginController");
            BufferedReader br = new BufferedReader(new FileReader(template));
            String line;

            Map<String, String> replaceWord = new HashMap<>(5);
            replaceWord.put("GroupId", project.getBackendProject().getProjectGroup());
            replaceWord.put("serviceNameEntity", serviceNameEntity);
            replaceWord.put("classNameEntity", classNameEntity);
            replaceWord.put("className", className);
            replaceWord.put("serviceName", serviceName);
            ProcessWriteLine processWriteLine = new ProcessWriteLine(replaceWord);
            while ((line = br.readLine()) != null) {
                processWriteLine.writeLine(line, writer);
            }
            br.close();

            writer.close();
            fileWriter.close();

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * @param project    :
     * @param loginTable : 登录表名
     * @return void
     * @author HXH
     * @description: 全局获取当前用户信息的方法
     * @date 2024/5/21
     **/
    @Override
    public void generationGetCurrentUserUtil(Project project, String loginTable) {
        try {
            ClassInfo loginUserClassInfo = GenerationTableInfo.getTableInfo(project, null, loginTable);
            String className = loginUserClassInfo.getClassName();
            String userEntityName = StringUtils.uncapitalize(className);

            File classFile = GenUtil.newFile(project, "config", "GetCurrentUserUtil");

            FileWriter fileWriter = new FileWriter(classFile);
            BufferedWriter writer = new BufferedWriter(fileWriter);

            File template = new File(Constants.templatePathPrefix + "simple\\template\\config\\GetCurrentUserUtil");
            BufferedReader br = new BufferedReader(new FileReader(template));
            String line;

            Map<String, String> replaceWord = new HashMap<>(3);
            replaceWord.put("GroupId", project.getBackendProject().getProjectGroup());
            replaceWord.put("userClassName",className);
            replaceWord.put("userEntityName",userEntityName);
            ProcessWriteLine processWriteLine = new ProcessWriteLine(replaceWord);
            while ((line = br.readLine()) != null) {
                processWriteLine.writeLine(line,writer);
            }
            br.close();

            writer.close();
            fileWriter.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
